package com.soul.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.soul.admin.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
@Slf4j
public class SystemController {

    @Autowired
    private AdminService adminService;

    /**
     * 后台登陆时 图片验证码 生成
     */
    @GetMapping("/verification")
    public void showValidateCodePic(String uuid, HttpServletRequest req, HttpServletResponse res){
        // res => response -> 给前端，给浏览器来读取
        res.setDateHeader("Expires",0); // Expires 失效的时间，0 立即失效，再次请求时要重新获取
        // Cache-Control：缓存控制：no-store不持久化，no-cache 不缓存,  must-revalidate 必须 使重新生效
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // addHeader 不会替换，而是追加。不需要检查, 直接发请求就行了 options
        res.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        res.setHeader("Pragma", "no-cache"); // 支持旧版本的浏览器, 不缓存
        // 兼容各种版本的浏览器，不要缓存图形验证码

        // 响应的内容体，是一个图片
        res.setContentType("image/jpeg");
        // 生成图形验证码图片，在内存
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);
        // 生成的验证码
        String code = lineCaptcha.getCode();
        log.debug("uuid={},code={}",uuid,code);
        // 保存到redis，有效期10分钟
        adminService.saveCode(uuid,code);
        try {
            // 把图片响应给浏览器
            lineCaptcha.write(res.getOutputStream());
        } catch (IOException e) {
            log.error("生成图形验证码图片失败!",e);
        }
    }

    /**
     *  后台系统登录
     * @param params 参数对象
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String,String> params){
        String username = params.get("username");
        String password = params.get("password");
        String verificationCode = params.get("verificationCode");
        String uuid = params.get("uuid");
        log.debug("用户名: {},密码: {},验证码: {},uuidKey: {}",username,password,verificationCode,uuid);
        return ResponseEntity.ok(adminService.login(username,password,verificationCode,uuid));
    }

    /**
     *  用户基本信息
     * @return
     */
    @RequestMapping(path = "/profile",method = RequestMethod.POST)
    public ResponseEntity profile(){
        return ResponseEntity.ok(adminService.profile());
    }

    /**
     *  后台系统退出
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity logout(@RequestHeader Map<String,String> params){
        adminService.logout(params.get("Authorization"));
        return ResponseEntity.ok(null);
    }
}